$(function() {
	movie_detail.run();
});

var movie_detail = {
	run : function() {
		this.__init__();
		this._start_();
		this._bind_events();
	},

	__init__ : function() {
		this.contextPath = $("#contextPath").val() || "";
		this.showId = $("#showId").val();
		this.type = $("#type").val();		
		this.itemUrl = $("#itemUrl").val();
		this.title = $("#title").val();
		this.source = $("#source").val();
		this.category = $("#category").val();
		this.vdInstalled = vd.check();
		this.genre ='';
		this.decade ='';

		side_pub._init_side_genre_decade(this);		
	},
	
	_start_ : function() {
		this._hightlight_nav();
		share.run();
	},
	
	_hightlight_nav: function(){
		var index = 0;		
		if(this.category == 'trailer'){
			index = 3;
		}else if(this.category == 'movie2012'){
			index = 2;
		}else if(this.category == 'full-length'){
			index = 1;
		}
		
		$("#movie_side_ul > li").eq(index).addClass("curr");
	},

	_bind_events : function() {
		// play now btn
		$("#play_btn").click(function() {
			var title = movie_detail.title;
			_gaq.push([ '_trackEvent', 'movie', 'Play', title ]);
			// main.addOrUpdateViewHistory(movie_detail.category,movie_detail.title,movie_detail.showId);
			window.location.href = movie_detail.contextPath + "/movie/" + movie_detail.category + "/watch-" + movie_detail.showId;
			return false;
		});
		
		$("#genre_div > div.vscroll-content > ul > li > a").click(function(){
			movie_detail.genre = $(this).attr("genre");
			movie_detail._genre_decade_relocation_event();
			return false;
		});			
		$("#decade_div > ul > li > a").click(function(){
			var decadeTemp = $(this).text();
			movie_detail.decade = decadeTemp.substring(0,4);
			movie_detail._genre_decade_relocation_event();
			return false;
		});

		var locaIndex = location.href.indexOf("#"); 
		var localUrl = location.href;
		if(locaIndex > 0){
			localUrl = localUrl.substring(0,locaIndex);
		}
		$("#fb-comments").attr("data-href", localUrl);		
	},
	
	_genre_decade_relocation_event: function(){
		var genre = movie_detail.genre;
		var decade = movie_detail.decade;
		var params = [];
		if(genre){
			params.push("genre=" + genre);
		}
		if(decade){
			params.push("decade=" + decade);
		}
		if(!genre && !decade){
			location.href = main.contextPath + "/movie/"+movie_detail.category;
			return false;
		}		
		location.href = main.contextPath + "/movie/"+movie_detail.category + "s#" + params.join("&");
	}
};

var share = {
	run : function() {
		// share btn
		$("#share_btn").click(function() {
			$("#share_fb_div").slideToggle();
			$("#share_fb_div > span.share-close").click(function() {
				$("#share_fb_div").slideUp();
			});
		});

		var url = location.href;
		$("#share_input").val(url);

		$("#fb_share_a").click(function() {
			var fbUrl = "https://www.facebook.com/sharer.php?u=" + encodeURIComponent(url);
			_gaq.push([ '_trackEvent', 'Movie', 'fb_share', url ]);
			window.open(fbUrl);
		});

		$("#tw_share_a").click(function() {
			var twUrl = "https://twitter.com/share?url=" + encodeURIComponent(url);
			_gaq.push([ '_trackEvent', 'Movie', 'twitter_share', url ]);
			window.open(twUrl);
		});
	}
};
